﻿using System.Collections.Generic;
using HMIS.FrameWork;
using HMIS.FrameWork.DomainBase;
using HMIS.FrameWork.Repository.Nhb;
using HMIS.Infrastructure.Core.Authority;

namespace HMIS.Infrastructure.Core.Repositories.Impl
{
    public class DepartmentRepositoryImpl : RepositoryNhbImpl<Department>, IDepartmentRepository
    {
        public override void SaveOrUpdate(Department entity)
        {
            if (IsFieldExist("Name", entity.Name, entity.Id))
                throw new ExistException();
            if (IsFieldExist("Code", entity.Code, entity.Id))
                throw new ExistException();

            base.SaveOrUpdate(entity);
        }
        public IList<Department> GetPlist(int start, int limit, string sort, string dir, List<DataFilter> filters, out long total)
        {
            var strFilter = base.GetHqlstrByExtFilter(filters,"d");

            sort = "d." + sort;

            var query = Session.CreateQuery(@"select d from Department as d"
                + (string.IsNullOrEmpty(strFilter) ? string.Empty : " where " + strFilter)
                + " order by " + sort + " " + dir)
                .SetFirstResult(start)
                .SetMaxResults(limit);

            total = Session.CreateQuery(@"select count(*) from Department as d "
                 + (string.IsNullOrEmpty(strFilter) ? string.Empty : " where " + strFilter)
                 )
                .UniqueResult<long>();

            return query.List<Department>();

        }

        public bool IsFieldExist(string fieldName, string fieldValue, string id)
        {
            return base.IsFieldExist(fieldName, fieldValue, id, null);
        }

        //模糊查询
		public IList<Department> QueryPlist(string query, int start, int limit, out long total)
		{
			//string sqlStr = string.IsNullOrEmpty(query) ? @" select s from Department as s" :
			string sqlStr = @" select s from Department as s 
						where lower(s.Name) like lower(:query) 
                            or lower(s.Code) like lower(:query)
                            or lower(s.InputCode1) like lower(:query) 
                            or lower(s.InputCode2) like lower(:query) 
                            or lower(s.InputCode3) like lower(:query)";

			var q = Session.CreateQuery(sqlStr).SetString("query", "%" + query + "%");
			if (limit > 0)
				q.SetFirstResult(start).SetMaxResults(limit);

			//string sumSql = string.IsNullOrEmpty(query) ? @"select count(*) from Department" :
			string sumSql = @" select count(*) from Department as s 
						where lower(s.Name) like lower(:query) 
                            or lower(s.Code) like lower(:query)
                            or lower(s.InputCode1) like lower(:query) 
                            or lower(s.InputCode2) like lower(:query) 
                            or lower(s.InputCode3) like lower(:query)";

			total = Session.CreateQuery(sumSql)
				.SetString("query", "%" + query + "%")
				.UniqueResult<long>();

			return q.List<Department>();
		}

        public override void Delete(string id)
        {
            if (IsHasUser(id))
                throw new ValidationException("所选机构下有用户!");
            base.Delete(id);
        }


        private bool IsHasUser(string id)
        {
            var query = Session.CreateQuery(@"select count(*)
                                                from Department as d 
                                               inner join d.Users u
                                               where d.Id=:Id ")
                .SetString("Id", id);
            return query.UniqueResult<long>() > 0;
        }


    }
}
